<script setup>
import { Plus } from '@element-plus/icons-vue'
import { ref, onMounted, watch } from 'vue'
import { getSongListDetail } from '@/api/song-list'
import { baseURL } from '@/api/request'
import EditSongList from './subs/EditSongList.vue'

const playlists = ref([])  // 歌单列表
const loading = ref(true)  // 加载状态
const error = ref(null)  // 错误信息
const searchQuery = ref('')  // 搜索查询

// 获取歌单数据
const fetchPlaylists = async () => {
  loading.value = true
  error.value = null
  try {
    const response = await getSongListDetail()

    if (response.code === '0' && Array.isArray(response.data)) {
      playlists.value = response.data.map(item => ({
        id: item.id,
        title: item.title,
        coverUrl: baseURL + '/r' + item.pic || '/default/cover.jpg',
        style: item.style,
        introduction: item.introduction
      }))
    } else {
      throw new Error(response.msg || '数据加载失败')
    }
  } catch (err) {
    error.value = err.message
    console.error('Failed to fetch song lists:', err)
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  fetchPlaylists()
})

// 监视搜索查询变化
watch(searchQuery, () => {
  fetchPlaylists() // 在搜索查询变化时刷新页面
})

// 抽屉控制
const editSongList = ref(null)

// 新建歌单方法
const addSongList = () => {
  if (editSongList.value) {
    editSongList.value.openDrawer()
  }
}

import { deleteSongList } from '@/api/song-list'
import { ElMessageBox, ElMessage } from 'element-plus'

// 删除歌单
const handleDelete = async (id) => {
  try {
    await ElMessageBox.confirm('确认删除该歌单？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })

    const res = await deleteSongList(id)
    if (res && res.code === '0') {
      ElMessage.success('删除成功')
      await fetchPlaylists() // 删除后刷新数据
    } else {
      ElMessage.error(res.msg || '删除失败')
    }
  } catch (error) {
    console.error('删除失败:', error)
  }
}

// 打开编辑抽屉
const openEditDrawer = (songList = null) => {
  if (editSongList.value) {
    editSongList.value.openDrawer(songList)
  }
}

import { useRouter } from 'vue-router'
const router = useRouter()

// 查看详情
const viewDetail = (id) => {
  router.push({ name: 'songlistdetail', params: { id } })
}

// 搜索过滤歌单
const searchPlaylists = () => {
  return playlists.value.filter(playlist => {
    const lowerQuery = searchQuery.value.toLowerCase()
    return playlist.title.toLowerCase().includes(lowerQuery) || playlist.style.toLowerCase().includes(lowerQuery)
  })
}
</script>

<template>
  <div class="featured-playlists">
    <el-row :gutter="20" align="middle" style="margin-bottom: 20px;">
      <el-col :span="12">
        <h2 class="section-title">精选歌单</h2>
      </el-col>
      <el-col :span="8">
        <el-input
          v-model="searchQuery"
          placeholder="搜索歌单"
          clearable
          style="width: 100%;"
        ></el-input>
      </el-col>
      <el-col :span="4" class="text-right">
        <el-button type="primary" @click="addSongList">
          新建歌单
          <el-icon><Plus /></el-icon>
        </el-button>
      </el-col>
    </el-row>

    <!-- 歌单列表 -->
    <el-row :gutter="80">
      <el-col :span="6" v-for="(playlist, index) in searchPlaylists()" :key="index">
        <el-card shadow="hover" class="playlist-card" @click="viewDetail(playlist.id)">
          <img :src="playlist.coverUrl" alt="歌单封面" class="cover-image" />
          <div class="playlist-info">
            <p class="title">{{ playlist.title }}</p>
            <p class="description">{{ playlist.style }}</p>
            <el-button @click.stop="openEditDrawer(playlist)" size="small">编辑</el-button>
            <el-button @click.stop="handleDelete(playlist.id)" size="small" type="danger">删除</el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 新增/编辑歌单抽屉 -->
    <EditSongList ref="editSongList" @saved="fetchPlaylists" />
  </div>
</template>

<style scoped>
.featured-playlists {
  padding: 20px;
}

.playlist-card {
  position: relative;
  margin-bottom: 20px;
  cursor: pointer;
}

.cover-image {
  width: 100%;
  height: auto;
  border-radius: 8px;
}

.play-count {
  position: absolute;
  bottom: 10px;
  right: 10px;
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  padding: 5px 10px;
  border-radius: 20px;
  font-size: 14px;
}

.playlist-info {
  text-align: center;
  margin-top: 10px;
}

.title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.description {
  font-size: 14px;
  color: #666;
}

.featured-playlists h2 {
  margin-bottom: 
}

.section-title {
  margin: 0;
  font-size: 24px;
  color: #333;
  font-weight: bold;
  border-left: 4px solid #409EFF;
  padding-left: 10px;
}

.text-right {
  text-align: right;
}
</style>
